package com.fc.im.netty.message;

import io.netty.buffer.ByteBuf;

import java.nio.ByteBuffer;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by ChanJiabo on 2017/1/17.
 */
public class MessageHeader {

    private static AtomicInteger requestId = new AtomicInteger(0);

    private int id = requestId.getAndIncrement();
    private byte checkCode;

    private byte version = 0x01;

    private byte msgType = 0;

    //加密方式
    private byte encryptType = 0x00;

    //压缩方式
    private byte compressType = 0x00;

    public static MessageHeader createHeader(ByteBuffer byteBuffer){
        MessageHeader header = new MessageHeader();
        header.checkCode = byteBuffer.get();
        header.version = byteBuffer.get();
        header.msgType = byteBuffer.get();
        header.encryptType = byteBuffer.get();
        header.compressType = byteBuffer.get();
        return header;
    }

    public int getId() {
        return id;
    }

    //通过header部分生成的checkcode
    public byte getCheckCodePart(){
        return (byte) (version ^ msgType ^ encryptType ^ compressType);
    }

    public byte getCheckCode() {
        return checkCode;
    }

    public byte getVersion() {
        return version;
    }

    public byte getMsgType() {
        return msgType;
    }

    public byte getEncryptType() {
        return encryptType;
    }

    public byte getCompressType() {
        return compressType;
    }

    public void setMsgType(byte msgType) {
        this.msgType = msgType;
    }
}
